var listPatients = [];
var listDistricts = [];
var listMedicines = [];

$(function() {
	$("#txtCreateDate").datepicker({dateFormat: "dd/mm/yy"});
	// get list city
	$('ul#listPatients li').each(function(index){
		var tmpObj = {};
		tmpObj.label = $(this).text();
		tmpObj.value = {
				dateOfBirth:$(this).data("patientdob"),
				id: $(this).val()
		};
		console.log(tmpObj.value);
		listPatients.push(tmpObj);
	});
	
	$('ul#listMedicines li').each(function(index){
		var tmpObj = {};
		tmpObj.id = $(this).val();
		tmpObj.price = $(this).data("pricevalue");
		console.log(tmpObj.price);
		listMedicines.push(tmpObj);
	});
	
	if($('#hiddenPatient').val() && $('#hiddenPatient').val() != '') {
		for(var i=0;i<listPatients.length;i++) {
			if(listPatients[i].value.id == $('#hiddenPatient').val()) {
				$('#txtSearchPatients').val(listPatients[i].label + " sinh ngày " + listPatients[i].value.dateOfBirth);
				break;
			}
		}
		
	}
	
	$('#txtSearchPatients').autocomplete({
	    source: listPatients,
	    focus: function( event, ui ) {
	        //$('#txtSearchPatients').val( ui.item.label );
	        return false;
	    },
	    select: function( event, ui ) {
	        //add your own action on item select!
	    	//console.log("select value = " + JSON.stringify(ui.item.value));
	        $('#hiddenPatient').val(ui.item.value.id);
	        $('#txtSearchPatients').val(ui.item.label + " sinh ngày " + ui.item.value.dateOfBirth);
	        return false;
	    }
	 
	});
	
	$('#addPrescription').click(addPrescription);
	/*
	.data( "autocomplete" )._renderItem = function( ul, item ) {
	    //define renderer for list
	return $( "<li></li>" )
	        .data( "item.autocomplete", item )
	        .append( "<a>" + item.label + "</a>" )
	        .appendTo( ul );
	};
	*/
});

function chooseMedicine(checkboxObj) {
	if($(checkboxObj).is(':checked')) {
		$('<input/>').attr({ type: 'text', id: 'number_' + $(checkboxObj).attr('id'), name: 'numberOfMedicine', onkeyup : 'calculateMoney()'}).appendTo($(checkboxObj).parent());
		$('<input/>').attr({ type: 'text', id: 'howtouse_' + $(checkboxObj).attr('id'), name: 'howToUse'}).appendTo($(checkboxObj).parent());
	} else {
		$("input#number_" + $(checkboxObj).attr('id')).remove();
		$("input#howtouse_" + $(checkboxObj).attr('id')).remove();
		calculateMoney();
	}
}

function calculateMoney() {
	console.log("****");
	var totalMoney = 0;
	if($('#hiddenCost').val() != '' && !isNaN($('#hiddenCost').val())) {
		 //totalMoney = new Number($('#hiddenCost').val());
	}
	$('ul#listMedicines').find('input[name="inputChooseMedicine"]').each(function(index ){
		if($(this).is(':checked')) {
			var id = $(this).attr('id');
			var arr = $.grep(listMedicines, function( obj , i ) {
			  return ( obj.id == id );
			});
			if(arr.length > 0) {
				console.log("**** price : " + parseInt(arr[0].price));
				console.log("**** quantity : " + parseInt($("input#number_" + $(this).attr('id')).val()));
				totalMoney += parseInt(arr[0].price)*parseInt($("input#number_" + $(this).attr('id')).val());
				console.log("**** totalMoney = : " + parseInt(arr[0].price)*parseInt($("input#number_" + $(this).attr('id')).val()));
			}
			
		}
	});
	$('#totalMoney').html(totalMoney);
	if($("#idTienKham").val() != "") {
		$('#hiddenCost').val(totalMoney + parseInt($("#idTienKham").val()));
	} else {
		$('#hiddenCost').val(totalMoney);
	}
	
	console.log("**** end");
}

function addPrescription() {
	var listChooseMedicine = [];
	// find checkbox checked on listMedicines ul
	$('ul#listMedicines').find('input[name="inputChooseMedicine"]').each(function(index ){
		if($(this).is(':checked')) {
			var saveObj = {
					id: $(this).attr('id'),
					cachsudung: $("input#howtouse_" + $(this).attr('id')).val(),
					soluong: $("input#number_" + $(this).attr('id')).val()
			};
			listChooseMedicine.push(saveObj);
		}
	});
	
	console.log("choose medicine list = " + JSON.stringify(listChooseMedicine));
	
	if(listChooseMedicine.length == 0) {
		return false;
	}
	
	var retObj = {data : listChooseMedicine};
	$('#prescriptionMedicine').val(JSON.stringify(retObj));
	
	return true;
}